package com.imooc.handle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.imooc.exception.GirlException;
import com.imooc.pojo.Result;
import com.imooc.result.ResultUtil;

/**
 * 统一异常处理
* @ClassName: ExceptionHandle 
* @Description: TODO 
* @author zyd
* @date 2017年12月12日 上午9:53:21
 */
@ControllerAdvice
public class ExceptionHandle {

	private final static Logger logger = LoggerFactory.getLogger(ExceptionHandler.class);
	//捕获异常
	@ExceptionHandler(value=Exception.class)
	@ResponseBody
	public Result handle(Exception e){
		if(e instanceof GirlException){
			GirlException girlException = (GirlException)e;
			return ResultUtil.error(girlException.getCode(), girlException.getMessage());
		}else{
			logger.error("【异常处理】 {}",e);
			return ResultUtil.error(-1, "未知错误");
		}
	}
}
